﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sl.Upload.Help
{
    public class FileHelp
    {
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="fileStream"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool SaveFile(byte[] byteFiles, string path)
        {
            bool ret = true;
            System.IO.MemoryStream m = null;
            System.IO.FileStream fl = null;
            try
            {

                //定义并实例化一个内存流，以存放提交上来的字节数组。
                m = new System.IO.MemoryStream(byteFiles);
                fl = new System.IO.FileStream(path, FileMode.Create);
                //把内内存里的数据写入物理文件
                m.WriteTo(fl);
                ret = true;
            }
            catch (Exception ex)
            {
                ret = false;
            }
            finally
            {
                if (m != null)
                {
                    m.Close();
                    m = null;
                }
                if (fl != null)
                {
                    fl.Close();
                    fl = null;
                }
            }
            return ret;
        }

        public void MergeFile()
        {
            //string fileName = fim.SavePath + "\\" + fim.Guid + fim.FileSuffix;
            //for (var i = 0; i < fim.Chunks; i++)
            //{
            //    FileStream addFile = new FileStream(fileName, FileMode.Append, FileAccess.Write);
            //    BinaryWriter AddWriter = new BinaryWriter(addFile);
            //    AddWriter.Write(File.ReadAllBytes(path + "\\" + i));
            //    AddWriter.Close();
            //    AddWriter.Dispose();
            //    addFile.Close();
            //    addFile.Dispose();
            //}
            //DirectoryInfo di = new DirectoryInfo(path);
            //di.Delete(true);
        }
    }
}
